<?php
/**
 * Created by PhpStorm.
 * User: Kevin
 * Date: 2019/6/17
 * Time: 14:01
 */
namespace app\admin\controller;
use think\Db;
class Cate extends Base
{
    public function lookOne(){
        if(request()->isAjax ()){
            extract (input ());
            $map = [];
            if(isset($key)&&$key!="")
            {
                $map['cate_name'] = ['like',"%" . $key . "%"];
            }
            $map['cate_level'] = 1;
            $Nowpage = input('page') ? input('page'):1;
            $limits = input("limit")?input("limit"):10;// 获取总条数;
            $count = Db::name('product_cate')
                ->where($map)
                ->count();
            $lists = Db::name('product_cate')
                ->where($map)
                ->page($Nowpage,$limits)
                ->order('cate_sort')
                ->select ();
            return json(['code'=>220,'msg'=>'','count'=>$count,'data'=>$lists]);
        }
        if(session("?cate_two")){
            session('cate_two',null);
        }
        return $this->fetch ('cate/look_one');
    }


    public function oneAdd(){
        if(request ()->isPost ()){
            extract (input());
            $param = [
                'cate_name'=>$name,
                'cate_pid'=>0,
                'cate_level'=>1,
                'cate_sort'=>$sort,
                'cate_add_time'=>date('Y-m-d H:i:s'),
                'cate_edit_time'=>date('Y-m-d H:i:s')
            ];
            $res = Db::name('product_cate')->insert($param);
            if($res){
                return json(['code'=>200,'msg'=>'添加成功']);
            }else{
                return json(['code'=>100,'msg'=>'添加失败']);
            }
        }
        return $this->fetch ('cate/one_add');
    }


    public function oneEdit(){
        extract (input());
        if(request ()->isPost ()){
            $param = [
                'cate_name'=>$name,
                'cate_sort'=>$sort,
                'cate_edit_time'=>date('Y-m-d H:i:s')
            ];
            $res = Db::name('product_cate')->where(['cate_id'=>$id])->update($param);
            if($res){
                return json(['code'=>200,'msg'=>'编辑成功']);
            }else{
                return json(['code'=>100,'msg'=>'编辑失败']);
            }
        }
        $data = Db::name('product_cate')->where(['cate_id'=>$id])->find();
        return $this->fetch ('cate/one_edit',['data'=>$data]);
    }


    public function oneDel(){
        extract (input());
        $all = Db::name('product_cate')->where(['cate_pid'=>$id,'cate_level'=>2])->column ('cate_id');
        $all[] = $id;
        $res = Db::name('product_cate')->where('cate_id','in',$all)->delete();
        if($res){
            return json(['code'=>200,'msg'=>'删除成功']);
        }else{
            return json(['code'=>100,'msg'=>'删除失败']);
        }
    }


    public function lookTwo(){
        extract (input());
        if(request()->isAjax ()){
            $map = [];
            if(isset($key)&&$key!="")
            {
                $map['cate_name'] = ['like',"%" . $key . "%"];
            }
            $map['cate_level'] = 2;
            $map['cate_pid'] = session('cate_two');
            $Nowpage = input('page') ? input('page'):1;
            $limits = input("limit")?input("limit"):10;// 获取总条数;
            $count = Db::name('product_cate')
                ->where($map)
                ->count();
            $lists = Db::name('product_cate')
                ->where($map)
                ->page($Nowpage,$limits)
                ->order('cate_sort')
                ->select ();
            return json(['code'=>220,'msg'=>'','count'=>$count,'data'=>$lists]);
        }
        if(!session("?cate_two")){
            session('cate_two',$id);
        }
        return $this->fetch ('cate/look_two');
    }

    public function twoAdd(){
        if(request ()->isPost ()){
            extract (input());
            $param = [
                'cate_name'=>$name,
                'cate_pid'=>session('cate_two'),
                'cate_level'=>2,
                'cate_sort'=>$sort,
                'cate_add_time'=>date('Y-m-d H:i:s'),
                'cate_edit_time'=>date('Y-m-d H:i:s')
            ];
            $res = Db::name('product_cate')->insert($param);
            if($res){
                return json(['code'=>200,'msg'=>'添加成功']);
            }else{
                return json(['code'=>100,'msg'=>'添加失败']);
            }
        }
        return $this->fetch ('cate/two_add');
    }

    public function twoEdit(){
        extract (input());
        if(request ()->isPost ()){
            $param = [
                'cate_name'=>$name,
                'cate_sort'=>$sort,
                'cate_edit_time'=>date('Y-m-d H:i:s')
            ];
            $res = Db::name('product_cate')->where(['cate_id'=>$id])->update($param);
            if($res){
                return json(['code'=>200,'msg'=>'编辑成功']);
            }else{
                return json(['code'=>100,'msg'=>'编辑失败']);
            }
        }
        $data = Db::name('product_cate')->where(['cate_id'=>$id])->find();
        return $this->fetch ('cate/two_edit',['data'=>$data]);
    }

    public function twoDel(){
        extract (input());
        $res = Db::name('product_cate')->delete($id);
        if($res){
            return json(['code'=>200,'msg'=>'删除成功']);
        }else{
            return json(['code'=>100,'msg'=>'删除失败']);
        }
    }
}